package com.mycode.skill.controller;

import cn.hutool.core.bean.BeanUtil;
import com.mycode.skill.commom.RestBean;
import com.mycode.skill.dto.SysUserDto;
import com.mycode.skill.entry.SysUser;
import com.mycode.skill.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * personController
 *
 * @author Qhy
 * @date 2023/12/1
 */
@RestController
public class SysUserController {
    @Autowired
    SysUserService sysUserService;

    @PostMapping("/person/save")
    public RestBean save(@RequestBody@Valid SysUserDto dto){
        SysUser person = BeanUtil.copyProperties(dto, SysUser.class);
        return RestBean.success( sysUserService.save(person));
    }

    @GetMapping("/ask-code")
    @Operation(summary = "请求邮件验证码")
    public RestBean<Void> askVerifyCode(@RequestParam @Email String email,
                                        @RequestParam @Pattern(regexp = "(register|reset)")  String type,
                                        HttpServletRequest request){

        sysUserService.registerEmailVerifyCode(type, String.valueOf(email), request.getRemoteAddr());
        return RestBean.success();
    }


}
